// Copyright 2022 jeff.li. and/or its affiliates.

#pragma once

#include <algorithm>
#include <cctype>
#include <memory>
#include <sstream>
#include <string>

#include <tbir/runtime/container/string.h>
#include <tbir/runtime/object.h>
#include <tbir/runtime/regex/regex_pattern.h>

namespace tbir::runtime {

    class RegexNode : public Object {
    public:
        RegexNode(const string_view &pattern,
                  bool ignore_case,
                  bool dotall,
                  bool extended,
                  bool anchored,
                  bool ucp);

        List Split(const string_view &input) const;

        List Split(const unicode_view &input) const;

        List Split(const Any &input) const;

        String Replace(const string_view &input, const string_view &repl) const;

        Unicode Replace(const unicode_view &input, const unicode_view &repl) const;

        RTValue Replace(const Any &input, const Any &repl) const;

        Tuple Match(const string_view &input, int64_t offset) const;

        Tuple Match(const unicode_view &input, int64_t offset) const;

        Tuple Match(const Any &input, int64_t offset) const;

        static constexpr const uint32_t _type_index = TypeIndex::kRuntimeRegex;
        static constexpr const char *_type_key = "Regex";
        TBIR_DECLARE_FINAL_OBJECT_INFO(RegexNode, Object);

    protected:
        std::shared_ptr<regex::RegexPattern> re_;
        String pattern_;

        bool ignore_case_;
        bool dotall_;
        bool extended_;
        bool anchored_;
        bool ucp_;

        unsigned int pcre_opt_;

        friend class Regex;

        friend struct ReprPrinter;
    };

}  // namespace tbir::runtime